package com.uzai.console.web.tb.tbtoken;

import com.alibaba.fastjson.JSONObject;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.console.annotation.SystemLogType;
import com.uzai.console.dto.tb.tbtoken.*;
import com.uzai.console.enums.OperationTypeEnum;
import com.uzai.console.enums.SystemModelEnum;
import com.uzai.console.service.tb.tbtoken.TbTokenService;
import com.uzai.console.vo.base.SelectIdVo;
import com.uzai.console.vo.takebal.redpacket.RedCardTempVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenAuthUrlVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenInfoVo;
import com.uzai.console.vo.tb.tbtoken.TbTokenPageVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 淘宝联盟
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "淘宝联盟",tags = "淘宝|淘宝联盟")
@ApiSort(9)
@RestController
public class TbTokenController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(TbTokenController.class);

    @Autowired
    private TbTokenService tbTokenService;

    @ApiOperation(value = "分页查询淘宝联盟列表", notes = "分页查询淘宝联盟列表", response = TbTokenPageVo.class)
    @ApiOperationSupport(order = 1)
    @PostMapping("/tbToken/findTbTokenList")
    public Object findTbTokenList(@RequestBody TbTokenQuery tbTokenQuery){
        //查询淘宝
        tbTokenQuery.setType(0);
        return tbTokenService.findTbTokenList(tbTokenQuery);
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "启用炮灰", notes = "启用炮灰")
    @ApiOperationSupport(order = 2)
    @PostMapping("/tbToken/setPh")
    public Object setPh(@RequestBody TbTokenSetPhDto tbTokenSetPhDto){
        tbTokenService.setPh(tbTokenSetPhDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "启用辅助", notes = "启用辅助" )
    @ApiOperationSupport(order = 3)
    @PostMapping("/tbToken/setAux")
    public Object setAux(@RequestBody TbTokenSetAuxDto tbTokenSeAuxDto){
        tbTokenService.setAux(tbTokenSeAuxDto);
        return true;
    }
    
    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.ADD)
    @ApiOperation(value = "添加联盟账号", notes = "添加联盟账号", response = TbTokenAuthUrlVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/tbToken/addToken")
    public Object addToken(@RequestBody TbTokenDto tbTokenDto){
        return tbTokenService.addTbToken(tbTokenDto);
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "重新授权", notes = "重新授权", response = TbTokenAuthUrlVo.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/tbToken/reauthorize")
    public Object reauthorize(@RequestBody TbTokenDto tbTokenDto){
        return tbTokenService.addTbToken(tbTokenDto);
    }

    @ApiOperation(value = "查询联盟详情", notes = "查询联盟详情", response = TbTokenInfoVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/tbToken/findTbTokenInfo")
    public Object findTbTokenInfo(@RequestBody TbTokenIdDto tbTokenIdDto){
        return tbTokenService.findTbTokenInfo(tbTokenIdDto);
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "设置推广位", notes = "设置推广位")
    @ApiOperationSupport(order = 7)
    @PostMapping("/tbToken/updatePid")
    public Object updatePid(@RequestBody TbTokenUpdatePidDto tbTokenUpdatePidDto){
        tbTokenService.updatePid(tbTokenUpdatePidDto);
        return true;
    }

    @ApiOperation(value = "更新联盟", notes = "更新联盟")
    @ApiOperationSupport(order = 7)
    @PostMapping("/tbToken/updateTbToken")
    public Object update(@RequestBody TbTokenUpdateDto tbTokenUpdateDto){
        if (Objects.isNull(tbTokenUpdateDto) || Objects.isNull(tbTokenUpdateDto.getId())) {
            throw new BusinessException("淘宝联盟更新参数错误");
        }
        return tbTokenService.update(tbTokenUpdateDto);
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.UPDATE)
    @ApiOperation(value = "是否同步订单", notes = "是否同步订单")
    @ApiOperationSupport(order = 8)
    @PostMapping("/tbToken/setIsSyncOrder")
    public Object setIsSyncOrder(@RequestBody TbTokenIsSyncOrderDto tbTokenIsSyncOrderDto){
        tbTokenService.setIsSyncOrder(tbTokenIsSyncOrderDto);
        return true;
    }

    @SystemLogType(model = SystemModelEnum.TbToken, type = OperationTypeEnum.DELETE)
    @ApiOperation(value = "删除", notes = "删除")
    @ApiOperationSupport(order = 9)
    @PostMapping("/tbToken/deleteTbToken")
    public Object deleteTbToken(@RequestBody TbTokenIdDto tbTokenIdDto){
        tbTokenService.deleteTbToken(tbTokenIdDto);
        return true;
    }

    @ApiOperation(value = "查询所有淘宝联盟列表", notes = "查询所有淘宝联盟列表", response = TbTokenInfoVo.class)
    @ApiOperationSupport(order = 10)
    @PostMapping("/tbToken/findTbTokenListByAll")
    public Object findTbTokenListByAll(@RequestBody TbTokenQuery tbTokenQuery){
        //查询淘宝
        tbTokenQuery.setType(0);
        return tbTokenService.findTbTokenListByAll(tbTokenQuery);
    }

    @ApiOperation(value = "查询淘宝联盟下拉列表", notes = "查询淘宝联盟下拉列表", response = SelectIdVo.class)
    @ApiOperationSupport(order=10)
    @PostMapping("/tbToken/findTbTokenSelectList")
    public Object findTbTokenSelectList(@RequestBody TbTokenQuery tbTokenQuery){
        //查询淘宝
        tbTokenQuery.setType(0);
        return tbTokenService.findTbTokenSelectList(tbTokenQuery);
    }

    @ApiOperation(value = "批量创建PID列表", notes = "批量创建PID列表", response = List.class)
    @ApiOperationSupport(order=11)
    @PostMapping("/tbToken/createPidList")
    public Object createPidList(@RequestBody CreatePidDto createPidDto){
        return tbTokenService.createPidList(createPidDto);
    }

}
